<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Payment_m extends CI_Model{
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    public function check_payment_of_new_mg($id_mg)
    {
        $sql="select * from new_money_per_test where id_mg = '$id_mg'";
        $query=$this->db->query($sql);
        return $query->result();
    }
    public function insert_new_money_per_test($id_mg,$new_money_per_test)
    {
        $data = array(   
                        'id_mg'=>$id_mg,
                        'new_money_per_test'=>$new_money_per_test
                       );
        $this->db->insert('new_money_per_test',$data);
    }
    public function create_CSV_payment_by_month($id_mg,$time)
    {
        $sql="select * from payment where id_mg = '$id_mg' and payment_month = '$time'";
        $query=$this->db->query($sql);
        return $query->result();
    }
    public function show_payment_by_id_mg($id_mg)
    {
        $sql="select * from payment where id_mg = '$id_mg'";
        $query=$this->db->query($sql);
        return $query->result();
    }
    public function get_money_per_test($id_mg)
    {
        $sql="select new_money_per_test from new_money_per_test where id_mg = '$id_mg'";
        $query=$this->db->query($sql);
        return $query->result();
    }
    public function show_date_test_by_id_manager($id_mg)
    {
        $space="";
        $sql="SELECT id_te,test_id,date_test FROM qsubmit WHERE id_mg='$id_mg' AND date_test<>'$space'  GROUP BY test_id,id_te";
        /*$this->db->select('id_te, date_test');
        $this->db->from('qsubmit');
        $this->db->where('id_mg',$id_mg);
        $this->db->where_not_in('date_test',$space);
        $this->db->group_by('test_id'); 
        $this->db->order_by('date_test', 'asc'); 
        $query=$this->db->get();*/
        $query=$this->db->query($sql);
        return $query;
    }
    public function update_db_to_list_payment($id_mg,$this_month,$total_submit_this_month,$money_per_test,$total_money)
    {
        $sql="UPDATE payment SET total_submit_this_month = '$total_submit_this_month', money_per_test = '$money_per_test', total_money = '$total_money' WHERE id_mg = '$id_mg' and payment_month = '$this_month'";
        $this->db->query($sql);
        //return $query->result();
    }
    public function create_payment_this_month($id_mg,$this_month,$total_submit_this_month,$money_per_test,$total_money,$address,$phone)
    {
        $data = array(   
                        'id_mg'=>$id_mg,
                        'payment_month'=>$this_month,
                        'total_submit_this_month'=>$total_submit_this_month,
                        'money_per_test'=>$money_per_test,
                        'total_money'=>$total_money,
                        'address'=>$address,
                        'phone'=>$phone
                       );
        $this->db->insert('payment',$data);
    }
    public function check_exist_payment_on_list($id_mg,$time)
    {
        $sql="select * from payment where id_mg = '$id_mg' and payment_month = '$time'";
        $query=$this->db->query($sql);
        return $query->result();
    }
    public function get_payment_time_from_manager($id_mg)
    {
        $this->db->select('year');
        $this->db->from('manager');
        $this->db->where('id_mg',$id_mg);
        $query=$this->db->get();
        return $query->result();
    }
    public function get_id_manager()
    {
        $this->db->select('id_mg');
        $this->db->from('manager');
        $query=$this->db->get();
        return $query->result();
    }
    public function view_payment_info($id)
    {  
          $this->db->select('*');
          $this->db->from('payment');
          $this->db->where('id_mg',$id);
          $query=$this->db->get();
          return $query;
    }
    public function get_payment_info($id)
    {  
          $this->db->select('*');
          $this->db->from('manager');
          $this->db->where('id_mg',$id);
          $query=$this->db->get();
          return $query->result();
    }
    public function update_payment_total_money($id_mg,$total_money)
    {
         $data = array(                    
                        'total_money'=>$total_money       
                );
        $this->db->where('id_mg', $id_mg);
        $this->db->update('payment', $data);
    }
    
    public function update_payment_ques_money($id_mg,$new_total_money)
    {
         $data = array(                    
                        'new_money_per_test'=>$new_total_money
                );
        $this->db->where('id_mg', $id_mg);
        $this->db->update('new_money_per_test', $data);
    }
     
}